
import JSONBig from 'json-bigint';
const baseUrl = 'https://api.daccf.com'; 
// const baseUrl = 'http://192.168.31.168:7001';



// let userToken = uni.getStorageSync('__userConfig').userServiceToken;
// let adminId = uni.getStorageSync('__userConfig').adminId;

let userToken = '';
let adminId = '';

export function setToken (userServiceToken, AdminId) {
	userToken = userServiceToken;
	adminId = AdminId;
}

export function request(options) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + options.url,
			data: options.data || {},
			method: options.method,
			header: {
				"testToken": "2gq72h2qrbhx256y0167uf5wd64ls55u",
				"BGDEBUG": 0,
				"requestType": "applet",
				"Authorization": uni.getStorageSync('__ddminiUserInfo').accessToken || "",
				"Content-Type": "application/x-www-form-urlencoded",
				"ADMIN_ID":adminId ,
				"user_service_token":userToken,
			},
			dataType: 'string',
			success(res) {
				let data =JSONBig.parse(res.data) ;
				resolve(data)
			},
		
		});
	});
}

export function server(options) {
	return new Promise((resolve, reject) => {
		options.data.userServiceToken = userToken;
		uni.request({
			url: baseUrl + options.url,
			data: options.data || {},
			method: options.method,
			header: {
				"testToken": "2gq72h2qrbhx256y0167uf5wd64ls55u",
				"BGDEBUG": 0,
				"requestType": "web",
				"Authorization": uni.getStorageSync('__ddminiUserInfo').accessToken || "",
				"Content-Type": "application/json",
				"ADMIN_ID":adminId ,
				"user_service_token":userToken,
			},
			dataType: 'string',
			success(res) {
				let data =JSONBig.parse(res.data) ;
				resolve(data)
			},
			
		});
	});
}
export function serverUpload(options) {
	let promise = new Promise((resolve,reject) => {
		let headers = {
			"testToken": "2gq72h2qrbhx256y0167uf5wd64ls55u",
			"BGDEBUG": 0,
			"requestType": "web",
			"Authorization": uni.getStorageSync('__ddminiUserInfo').accessToken || "",
			"Content-Type": "application/json",
			"ADMIN_ID": '158',
			"user_service_token": userToken
		};
		uni.uploadFile({
			url: baseUrl + options.url,
			filePath: options.data.file,
			formData: options.data || {},
			name: 'file',
			dataType:'string',
			header: headers,
			success: function(res) {
				let data =JSONBig.parse(res.data);
				if (data.code == 200) {
					resolve(data);
				} else if (data.code == 11001) {
					uni.showToast({
						icon: 'none',
						title: '登陆过期，请重新登陆'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/subpackagef/mycenter/login'
						})
					}, 1000)
				} else {
				 // uni.showToast({
					// 	icon: 'none',
					// 	title: data.msg
					// })
				}
			},
			fail(res) {
				// 失败处理
				reject(res);
			}
		});
	});
	return promise;
}



